package com.cloud.class05.exp;

import com.cloud.class05.resp.ResultData;
import com.cloud.class05.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2024/4/9 17:09
 */
@Slf4j
//@ControllerAdvice的作用，全局异常处理
@RestControllerAdvice//@ControllerAdvice+@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> exception(Exception e) {
        System.out.println("全局异常处理");
        log.error("全局异常处理:{}", e.getMessage(), e);
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }
}
